<?php
final class User {
	private $user_id;
	private $account;
	private $location;
	private $description;
	private $last_key;
	
  	public function __construct($registry) {
		$this->config = $registry->get('config');
		$this->db = $registry->get('db');
		$this->request = $registry->get('request');
		$this->session = $registry->get('session');
		
		if(isset($registry->get('session')->data['last_key'])){
			$lastkey=$registry->get('session')->data['last_key'];
			
			if(isset($lastkey['error'])){
					$registry->get('session')->data['warning']=$lastkey['error_CN'];
			}else{
				
				if(!isset($this->session->data['umsg'])){
					$registry->set('weiboclient',new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret'] ));
					$umsg=$registry->get('weiboclient')->verify_credentials();
					$this->session->data['umsg']=$umsg;	
					$asyntask=new AsynTask('grab/logingrab',$_SESSION['last_key']);
					$asyntask->execute();
				}else{
					
					$umsg=$this->session->data['umsg'];
				}
						

					if (isset($umsg['id'])){
						$this->user_id = $umsg['id'];
					}
					
					if (isset($umsg['name'])){
						$this->account=$umsg['name'];
					}
					if (isset($umsg['location'])){
						$this->location=$umsg['location'];
					}
					if (isset($umsg['description'])){
						$this->description=$umsg['description'];
					}			
		  }
		} 
	}
		
  	public function login($userdata) {
		
		if ($userdata) {
			$this->session->data['last_key'] = $userdata['last_key'];	
		   
			$this->user_id=$userdata['last_key'];
	  		return true;
    	} else {
      		return false;
    	}
  	}
  
  	public function logout() {
		unset($this->session->data['$last_key']);

		$this->user_id = '';
		$this->account='';
		$this->location='';
		$this->description='';
		
		session_destroy();
  	}
  
  	public function isLogged() {
    	return $this->user_id;
  	}

  	public function getId() {
    	return $this->user_id;
  	}
      
  	public function getAccount() {
		return $this->account;
  	}
  
  	public function getTelephone() {
		return $this->telephone;
  	}
  	
  	public function getLocation() {
		return $this->location;
  	}
  	
  	public function getDescription() {
		return $this->description;
  	}
  
}
?>